package com.zxl.yjb.fragment.home.sharemessage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.sharemessage.MessageListActivity;
import com.zxl.yjb.activity.home.sharemessage.MessagePublishActivity;
import com.zxl.yjb.activity.home.sharemessage.ShareMessageClassifyActivity;
import com.zxl.yjb.adapter.ShareMsgMenuAdapter;
import com.zxl.yjb.base.LazyBaseFragment;
import com.zxl.yjb.bean.ShareMsgClassifyBean;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.widget.PagerRecyclerView.DividerGridItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;

public class ShareMessageMenuFragment extends LazyBaseFragment {


    @BindView(R.id.rv_sharemessage_classify)
    RecyclerView rv_sharemessage_classify;

    private ShareMsgMenuAdapter shareMsgMenuAdapter;
    private GridLayoutManager gridLayoutManager;
    private DividerGridItemDecoration dividerItemDecoration;
    private ArrayList<ShareMsgClassifyBean.ShareMsgMenuBean> shareMsgMenuBeans;
    private ShareMessageClassifyActivity activity;
    private String origin = "";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sharemessage_classify;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

    }

    public void setData(ArrayList<ShareMsgClassifyBean.ShareMsgMenuBean> shareMsgMenuBeans)
    {
        this.shareMsgMenuBeans = shareMsgMenuBeans;
    }

    public void setOrigin(String origin)
    {
        this.origin = origin;
    }

    @Override
    protected void main() {
        super.main();
        activity = (ShareMessageClassifyActivity) mActivity;
        gridLayoutManager = new GridLayoutManager(getActivity(),3);
        rv_sharemessage_classify.setLayoutManager(gridLayoutManager);
        dividerItemDecoration = new DividerGridItemDecoration(getActivity(), DisplayUtil.dip2px(getActivity(),5),R.color.tab_color);
        rv_sharemessage_classify.addItemDecoration(dividerItemDecoration);
        shareMsgMenuAdapter = new ShareMsgMenuAdapter(R.layout.item_skill,shareMsgMenuBeans);
        rv_sharemessage_classify.setAdapter(shareMsgMenuAdapter);
        shareMsgMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (origin.equals(MessagePublishActivity.class.getName()))
                {
                    Intent intent = new Intent();
                    intent.putExtra("name",shareMsgMenuBeans.get(position).getName());
                    intent.putExtra("id",shareMsgMenuBeans.get(position).getId());
                    getActivity().setResult(Activity.RESULT_OK,intent);
                    getActivity().finish();
                }
                else
                {
                    Intent intent = new Intent(activity, MessageListActivity.class);
                    intent.putExtra("name",shareMsgMenuBeans.get(position).getName());
                    intent.putExtra("id",shareMsgMenuBeans.get(position).getId());
                    startActivity(intent);
                }
            }
        });
    }

}
